package com.kaishengit.service;


import java.util.List;

import org.springframework.transaction.annotation.Transactional;

import com.kaishengit.dao.TelDao;
import com.kaishengit.pojo.Company;
import com.kaishengit.pojo.Contacts;
import com.kaishengit.pojo.Tel;
import com.kaishengit.util.DateUtil;

@Transactional
public class TelService {
	private TelDao telDao;
	
	public void savephone(String[] tel2, String[] teltype, Contacts contacts) {
		telDao.delAll(findByObject(contacts));
		if(tel2 != null && teltype != null) {
			for(int i = 0; i < tel2.length; i ++) {
				for(int j = 0; j < teltype.length; j ++) {
					if(i == j) {
						Tel tel = new Tel();
						tel.setTel(tel2[i]);
						tel.setType(teltype[j]);
						tel.setCreatetime(DateUtil.getNow());
						tel.setContacts(contacts);
						telDao.save(tel);
					}
				}
			}
		}
		
	}

	
	
	//  set   get.........................................
	public TelDao getTelDao() {
		return telDao;
	}

	public void setTelDao(TelDao telDao) {
		this.telDao = telDao;
	}



	public void savephone(String[] tel2, String[] teltype, Company company) {
		if(tel2 != null && teltype != null) {
			for(int i = 0; i < tel2.length; i ++) {
				for(int j = 0; j < teltype.length; j ++) {
					if(i == j) {
						Tel tel = new Tel();
						tel.setTel(tel2[i]);
						tel.setType(teltype[j]);
						tel.setCreatetime(DateUtil.getNow());
						
						telDao.save(tel);
					}
				}
			}
		}
	}



	public List<Tel> findByObject(Contacts contacts) {
		String hql = "select h from Tel as h where h.contacts.id = ?";
		return telDao.find(hql, contacts.getId());
	}



	public List<Tel> findByObject(Company company) {
		String hql = "from Tel where tel.company.id = ?";
		return telDao.find(hql, company.getId());
	}



	public void delphone(Contacts contacts) {
		telDao.delAll(findByObject(contacts));
	}



}




	

	
	
